package com.dfqy.controller;

import com.dfqy.pojo.*;
import com.dfqy.service.JoinService;
import com.dfqy.vo.Page;
import com.dfqy.vo.Result;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Author : atyushu
 * Package : com.dfqy.controller
 * ClassName : JoinController
 * Create : 2023/8/2 - 19:49
 * Version : v1.0
 * Description : The class of controller for join.
 */
@Validated
@RestController
@RequestMapping("join")
public class JoinController {
    @Autowired
    private JoinService joinService;

    @InitBinder
    public void initBinder(WebDataBinder webDataBinder) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        simpleDateFormat.setLenient(false);
        webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(simpleDateFormat, true));
    }

    @GetMapping("joinPage")
    public Page<Join> page(@RequestParam(name = "page", defaultValue = "1") @Range(min = 1, message = "页码过小") int page,
                           @RequestParam(name = "limit", defaultValue = "10") @Range(min = 1, message = "条数过小") int limit,
                           @RequestParam(name = "jointimeStart", required = false) Date jointimeStart,
                           @RequestParam(name = "jointimeEnd", required = false) Date jointimeEnd,
                           @RequestParam(name = "phone", required = false) String phone,
                           @RequestParam(name = "uname", required = false) String uname,
                           @RequestParam(name = "isattend", required = false, defaultValue = "-1") Byte isattend,
                           @RequestParam(name = "eventid", required = false, defaultValue = "0") Long eventid
    ) {
        return joinService.page(page, limit, jointimeStart, jointimeEnd, phone, uname, isattend, eventid);
    }

    @GetMapping("deleteJoin/{id}")
    public Result delete(@PathVariable long id) {
        int delete = joinService.delete(id);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @GetMapping("batchDeleteJoin/{id}")
    public Result batchDelete(@PathVariable String id) {
        String[] idStr = id.split(",");
        long[] ids = new long[idStr.length];
        for (int i = 0; i < idStr.length; i++) {
            ids[i] = Long.parseLong(idStr[i]);
        }
        int delete = joinService.delete(ids);
        return delete > 0 ? Result.success("删除成功") : Result.error("删除失败");
    }

    @PostMapping("updateJoin")
    public Result update(@RequestBody Join join) {
        int update = joinService.update(join);
        return update > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("getJoinById/{id}")
    public Result getById(@PathVariable long id) {
        Join join = joinService.getById(id);
        return join != null ? Result.success(join) : Result.error("无数据");
    }

    @GetMapping("absent/{id}")
    public Result absent(@PathVariable long id) {
        int absent = joinService.absent(id);
        return absent > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("attend/{id}")
    public Result attend(@PathVariable long id) {
        int attend = joinService.attend(id);
        return attend > 0 ? Result.success("修改成功") : Result.error("修改失败");
    }

    @GetMapping("getByEventId/{eventid}")
    public Page<Join> getByEventId(@RequestParam(name = "page", defaultValue = "1") @Range(min = 1, message = "页码过小") int page,
                                   @RequestParam(name = "limit", defaultValue = "10") @Range(min = 1, message = "条数过小") int limit,
                                   @PathVariable long eventid
    ) {
        return joinService.getByEventId(page, limit, eventid);
    }
}